
package com.jf.cloud.product.listener;

import com.jf.cloud.api.product.dto.SkuStockLockDTO;
import com.jf.cloud.common.order.vo.ShopCartItemDiscountVO;
import com.jf.cloud.common.order.vo.ShopCartItemVO;
import com.jf.cloud.common.order.vo.ShopCartOrderVO;
import com.jf.cloud.common.product.constant.StockBillType;
import com.jf.cloud.common.product.constant.StockType;
import com.jf.cloud.common.product.vo.GiveawaySpuVO;
import com.jf.cloud.common.product.vo.GiveawayVO;
import com.jf.cloud.common.rocketmq.config.RocketMqConstant;
import com.jf.cloud.product.service.StockBillLogService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 解锁库存的监听
 * @author zz
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.OUT_STOCK_LOG_TOPIC,consumerGroup = RocketMqConstant.OUT_STOCK_LOG_TOPIC)
public class OutStockLogConsumer implements RocketMQListener<List<ShopCartOrderVO>> {

    @Autowired
    private StockBillLogService stockBillLogService;

    /**
     * 1、添加出库明细
     */
    @Override
    public void onMessage(List<ShopCartOrderVO> shopCartOrders) {
        List<SkuStockLockDTO> skuStockLocks = new ArrayList<>();

        for (ShopCartOrderVO shopCartOrder : shopCartOrders) {
            List<ShopCartItemDiscountVO> shopCartItemDiscounts = shopCartOrder.getShopCartItemDiscounts();
            for (ShopCartItemDiscountVO shopCartItemDiscount : shopCartItemDiscounts) {
                List<ShopCartItemVO> shopCartItems = shopCartItemDiscount.getShopCartItems();
                for (ShopCartItemVO orderItem : shopCartItems) {
                    if (!Objects.equals(orderItem.getSupplierId(),0L) && Objects.equals(orderItem.getSupplierDeliveryType(),1)) {
                        skuStockLocks.add(new SkuStockLockDTO(orderItem.getShopId(), orderItem.getSupplierSpuId(), orderItem.getSupplierSkuId(), shopCartOrder.getOrderId(), orderItem.getCount(), -1,orderItem.getSpuId()));
                        continue;
                    }
                    skuStockLocks.add(new SkuStockLockDTO(orderItem.getShopId(), orderItem.getSpuId(), orderItem.getSkuId(), shopCartOrder.getOrderId(), orderItem.getCount(), -1,null));
                    //处理赠品订单项
                    GiveawayVO giveaway = orderItem.getGiveaway();
                    if (Objects.isNull(giveaway)) {
                        continue;
                    }
                    // 赠品库存计算
                    for (GiveawaySpuVO giveawaySpuVO : giveaway.getGiveawaySpus()) {
                        skuStockLocks.add(new SkuStockLockDTO(giveawaySpuVO.getShopId(),giveawaySpuVO.getSpuId(), giveawaySpuVO.getSkuId(), shopCartOrder.getOrderId(), giveawaySpuVO.getGiveawayNum(), -1, null));
                    }
                }
            }
        }
        long count = stockBillLogService.countByOrderId(skuStockLocks.get(0).getOrderId(),StockType.OUT_OF_STOCK.value());
        if(count > 0){
            return;
        }
        stockBillLogService.generateStockLogOfOrderList(skuStockLocks, StockBillType.PURCHASE_OUTBOUND.value(), StockType.OUT_OF_STOCK.value());
    }
}
